<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鼠标跟随生成艺术</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
            background: #000;
            font-family: Arial, sans-serif;
            overflow: hidden;
        }
        .controls {
            position: absolute;
            top: 10px;
            left: 10px;
            color: white;
            background: rgba(0, 0, 0, 0.7);
            padding: 10px;
            border-radius: 5px;
            font-size: 12px;
            line-height: 1.4;
            z-index: 1000;
        }
    </style>
</head>
<body>
    <div class="controls">
        <div>移动鼠标控制图案中心</div>
        <div>按键: 1-白色 2-彩虹 3-蓝色 4-暖色</div>
        <div>+/-: 调节速度 []: 调节点数</div>
        <div>R: 重置 C: 清除 S: 保存</div>
    </div>

    <script>
        let t = 0;
        let mouseTargetX = 400;
        let mouseTargetY = 300;
        let currentMouseX = 400;
        let currentMouseY = 300;
        
        let colorMode = 0;
        let hueOffset = 0;
        let pointCount = 3000;
        let trailAlpha = 10;
        let speedMultiplier = 1.0;
        let scaleMultiplier = 1.0;
        
        function setup() {
            createCanvas(windowWidth, windowHeight);
            background(9);
            stroke(255, 96);
            strokeWeight(1);
            noFill();
            
            mouseTargetX = width / 2;
            mouseTargetY = height / 2;
            currentMouseX = mouseTargetX;
            currentMouseY = mouseTargetY;
        }
        
        function draw() {
            fill(9, trailAlpha);
            noStroke();
            rect(0, 0, width, height);
            
            updateColor();
            strokeWeight(1);
            
            currentMouseX = lerp(currentMouseX, mouseTargetX, 0.1);
            currentMouseY = lerp(currentMouseY, mouseTargetY, 0.1);
            
            t += (PI / 120) * speedMultiplier;
            hueOffset += 0.5;
            
            for (let i = 0; i < pointCount; i++) {
                drawArtPoint(i / 345.0);
            }
            
            drawMouseIndicator();
        }
        
        function updateColor() {
            switch(colorMode) {
                case 0:
                    stroke(255, 96);
                    break;
                case 1:
                    colorMode(HSB, 360, 100, 100);
                    stroke((hueOffset + t * 50) % 360, 80, 100, 96);
                    colorMode(RGB, 255);
                    break;
                case 2:
                    stroke(100, 150, 255, 96);
                    break;
                case 3:
                    stroke(255, 150, 100, 96);
                    break;
            }
        }
        
        function drawArtPoint(y) {
            let k;
            if (y < 11) {
                k = (6 + sin(Math.pow(y, 8)) * 6) * cos(t - t/4);
            } else {
                k = (y/5 + cos(y/2)) * cos(t - t/4);
            }
            
            let e = y/7 - 13;
            let d = sqrt(k*k + e*e) + sin(e/4 + t)/2;
            let c = d/2 + 1 - t/2;
            let q = y * k / d * (3 + sin(d*2 + y/2 - t*4));
            
            q *= scaleMultiplier;
            
            let x = q + 60 * cos(c) + currentMouseX;
            let yPos = q * sin(c) + d * 29 - 170 + currentMouseY;
            
            point(x, yPos);
        }
        
        function drawMouseIndicator() {
            push();
            noFill();
            stroke(255, 50);
            strokeWeight(1);
            
            line(currentMouseX - 20, currentMouseY, currentMouseX + 20, currentMouseY);
            line(currentMouseX, currentMouseY - 20, currentMouseX, currentMouseY + 20);
            ellipse(currentMouseX, currentMouseY, 30, 30);
            pop();
        }
        
        function mouseMoved() {
            mouseTargetX = mouseX;
            mouseTargetY = mouseY;
        }
        
        function mouseDragged() {
            mouseTargetX = mouseX;
            mouseTargetY = mouseY;
        }
        
        function keyPressed() {
            if (key === 'r' || key === 'R') {
                background(9);
            } else if (key === 'c' || key === 'C') {
                background(9);
                t = 0;
                speedMultiplier = 1.0;
                scaleMultiplier = 1.0;
                pointCount = 3000;
                trailAlpha = 10;
            } else if (key === 's' || key === 'S') {
                save('mouse_following_art_' + year() + month() + day() + '_' + hour() + minute() + second() + '.png');
            } else if (key === '1') {
                colorMode = 0;
            } else if (key === '2') {
                colorMode = 1;
            } else if (key === '3') {
                colorMode = 2;
            } else if (key === '4') {
                colorMode = 3;
            } else if (key === '+' || key === '=') {
                speedMultiplier = min(speedMultiplier + 0.2, 5.0);
            } else if (key === '-' || key === '_') {
                speedMultiplier = max(speedMultiplier - 0.2, 0.1);
            } else if (key === '[') {
                pointCount = max(pointCount - 500, 500);
            } else if (key === ']') {
                pointCount = min(pointCount + 500, 10000);
            } else if (key === ',') {
                trailAlpha = max(trailAlpha - 5, 5);
            } else if (key === '.') {
                trailAlpha = min(trailAlpha + 5, 50);
            }
        }
        
        function mouseWheel(event) {
            let e = event.delta;
            if (e < 0) {
                scaleMultiplier = min(scaleMultiplier + 0.1, 3.0);
            } else {
                scaleMultiplier = max(scaleMultiplier - 0.1, 0.1);
            }
        }
        
        function windowResized() {
            resizeCanvas(windowWidth, windowHeight);
        }
    </script>
</body>
</html>